package org.blog.controller;

import org.blog.entity.Admin;
import org.blog.entity.param.AdminParam;
import org.blog.entity.param.UpdateAdminParam;
import org.blog.service.AdminService;
import org.blog.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;



    @GetMapping("index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/index");

        // 根据参数获取admin
        List<Admin> list = adminService.getAdmins(new AdminParam());
        modelAndView.addObject("list", list);
        return modelAndView;
    }

    @GetMapping("toLogin")
    public String toLogin() {
        return "admin/adminLogin";
    }

    @PostMapping("login")
    @ResponseBody
    public ResultResponse login(Admin admin, HttpServletRequest request) {
        System.out.println("login");
        Admin admin1 = adminService.selectByAdminName(admin.getAdminName());
        if (admin1 == null) {
            return new ResultResponse(false, "不允许登录");
        } else {
            String adminPassword = admin.getAdminPassword();
            System.out.println(adminPassword);
            System.out.println(admin1.getAdminPassword());
            if (!adminPassword.equals(admin1.getAdminPassword())) {
                return new ResultResponse(false, "账户名或密码错误");
            } else {
                request.getSession().setAttribute("list", admin1);
                return new ResultResponse(true, "登录成功");

            }

        }
    }


    @GetMapping("toChange/{adminId}")
    public void toChange(@PathVariable("adminId") Integer adminId, HttpServletRequest request, HttpServletResponse response) {

        Admin admin = adminService.getAdminById(adminId);
        request.setAttribute("admin", admin);
        try {
            request.getRequestDispatcher("/admin/adminChange.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @PostMapping("change")
    @ResponseBody
    public ResultResponse change(@RequestBody UpdateAdminParam admin) {
        if (admin == null) {
            return new ResultResponse(false, "参数错误");
        }

        System.out.println(admin.toString());
        Admin admin1 = adminService.getAdminById(admin.getAdminId());
        if (admin1 == null) {
            return new ResultResponse(false, "用户不存在");
        } else {
            if (!admin1.getAdminPassword().equals(admin.getAdminOldPassword())) {
                return new ResultResponse(false, "密码错误");
            }
        }

        admin1.setAdminName(admin.getAdminName());
        admin1.setAdminPassword(admin.getAdminNewPassword());

        boolean update = adminService.update(admin1);
        if (update) {
            return new ResultResponse(true, "修改成功");
        } else {
            return new ResultResponse(false, "修改失败");
        }
    }

    @GetMapping("delete/{adminId}")
    public void delete(@PathVariable("adminId") Integer adminId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean delete = adminService.delete(adminId);
        if (delete) {
            response.sendRedirect("/admin/index");
        } else {
            response.getWriter().println("删除失败");
            response.getWriter().close();
        }
    }


    @PostMapping("search")
    public ModelAndView search( AdminParam param) {
        System.out.println(param.toString());
        ModelAndView modelAndView = new ModelAndView("admin/index");
        List<Admin> admins = adminService.getAdmins(param);
//        System.out.println(admins.get(0).getAdminName());

        modelAndView.addObject("list", admins);
        return modelAndView;
    }
}
